package dao.impl;

import dao.PublisherDao;
import pojo.Publisher;
import util.JDBCUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Tiler on 2020/11/4
 */
public class PublisherDaoImpl implements PublisherDao {
    @Override
    public List<Publisher> selectAllPublishers() {
        List<Publisher> publishers = new ArrayList<>();

        String sql = "select * from publisher";

        JDBCUtil jdbcUtil = new JDBCUtil();

        ResultSet resultSet = jdbcUtil.excuteQuery(sql);

        try {
            while(resultSet.next()) {
                Publisher publisher = new Publisher();

                publisher.setPublisherId(resultSet.getInt("publisherId"));
                publisher.setPublisherName(resultSet.getString("publisherName"));

                publishers.add(publisher);
            }

            return publishers;
        } catch (SQLException e) {
            e.printStackTrace();

            return null;
        }
        finally {
            jdbcUtil.close();
        }
    }
}
